import yaml  
  
def read_config(yaml_file)->dict:  
    with open(yaml_file, 'r') as file:  
        data = yaml.safe_load(file)  
    return dict(data)

def cacu_mid(smid:str):
    smid = smid.split('.')
    num = 0
    for x in smid:
        num = num*256 + int(x)
    return hex(num)

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='generate code for device table')
    # Add optional args and there definitions
    parser.add_argument('--board', type=str, default='?', help='specify the config')
    args = parser.parse_args()

    relativep = 'devifc'
    dev_models = read_config('{}/staticmid.yaml'.format(relativep))
    dev_defs   = read_config('{}/{}.dvc'.format(relativep,args.board))

    print('#include "astraldef.h"')

    for d in dev_defs.keys():
        dev = dev_defs[d]
        md = dev.get('model','-')
        if md == '-': continue
        dev['model'] = dev_models.get(md, '?')
        md = (md)
        # print(dev)
        code = 'SREGIDEV({},{},{},{})'.format(
            hex(dev['base']),
            dev['irq'],
            d,
            cacu_mid(dev['model'])
        )
        print(code)
